import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.junit.Test;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.exception.BirdRequestException;
import com.vcredit.creditcat.exception.ProductCanNotApplyException;
import com.vcredit.creditcat.util.RSAUtil;
import com.vcredit.creditcat.vo.product.hl.HlReturnInfoVo;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

public class HLApplyService {
	private String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+hHZWDIbKVpqg0r3T+GuomkzZG+RI7vQDttkyGguURo0gJGIKcTFj0Cw7vAbUv6TMlmtTA054DaBd247aVPcSWJdmifNL1v7KLuGDDJ+42jrMkZ+SHj7SulLNdnMFPlW5WjCsA0Co5kRpD/MTTsu73CPe68Nq6Eb74yxQU9xKewIDAQAB";
	private String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL6EdlYMhspWmqDSvdP4a6iaTNkb5Eju9AO22TIaC5RGjSAkYgpxMWPQLDu8BtS/pMyWa1MDTngNoF3bjtpU9xJYl2aJ80vW/sou4YMMn7jaOsyRn5IePtK6Us12cwU+VblaMKwDQKjmRGkP8xNOy7vcI97rw2roRvvjLFBT3Ep7AgMBAAECgYA4EtKlhvBRM2Y1eXFnpxFAwZFmwT/Se4oUYcfRaDsKPC2jTTIR8rjfsNCkEDZOfc1qbcdO2oVI7nHJzipgdmt1V7PdPTUH5na5sIq7/TX0iTRuhQyDD3tSlP0R31yOUpAr0vZzH0+ISR5tqNphqLq4jUDFTgf0+B6C+/M7oWTacQJBAOhzVyylQitEoi84yAOSgAYnRm65HgYG9lSrlPBabMwXtiFEwuH4nh9BIrI6NTy/oWwZ850o+Bh3MAa/iX5Pt1MCQQDR0ZOTy5nZZoPnBV/EDV8P+O5QTXsrJul3N2hZhg1lWUfxKD5CMzlW7EBPd7j80hM+owPxMVy9XusOk4Jo34M5AkAPdQwI9fxOcEPwq+/w/N7yic4PMtQiRng+7SH8UacGc1UzJSVobWXmYihyfZhor72GOLuQadcFmXdiG36DHb3tAkEAnFCPqsc/ikimsiE91HjFvseM/ESNI3vs6UVdN3z4U+4s+8woffddm5WM3+do6Om1LrASCsnktqyVYBmjSXBjeQJBAOWGOKLb2NG29IBSmm2x7fTac0pjGPZfWs3BV2CAPY/t1GhT9I+ksKB1ubuH+aRZfu/YqStamYyXUAEGMhyaa3o=";

	private String encodeJsonParam(String jsonParam) throws UnsupportedEncodingException, Exception {
		// 加密
		String requestContentBase64 = Base64.encodeBase64String(RSAUtil.encryptByPrivateKey(jsonParam.getBytes("utf-8"), this.privateKey));
		return requestContentBase64;
	}

	private String decodeJsonParam(String respJson) throws Exception {
		String result = "";
		// 解密
		byte[] res = RSAUtil.decryptBASE64(respJson);
		result = new String(RSAUtil.decryptByPublicKey(res, this.publicKey));
		return result;
	}

	/**
	 * 是否可交单
	 * 
	 * @param submitDto
	 * @return
	 * @throws Exception
	 */
	@Test
	public void comitOrNot() throws UnsupportedEncodingException, Exception {
		@SuppressWarnings("unused")
		HlReturnInfoVo authPersonal = null;
		String url = "http://218.104.220.30:9999/api-facade/";
		try {
			Map<String, Object> ma = new HashMap<String, Object>();
			ma.put("identityNo", "231081199012020029");// 范阳612727199405170038';362522198907295026#高雪
														// 231081199012020029
			ma.put("userName", "高雪");
			ma.put("facePairResult", "65");
			ma.put("productType", "XYM");
			// logger.info(creditHLUrl.concat("check/isReceiveCustomers"));
			// 加密请求、
			String json = JacksonUtil.Obj2Json(ma);
			String code = this.encodeJsonParam(json);
			String resultJson = HttpRequest.doJson(url.concat("check/isReceiveCustomers"), code);
			// 解密数据
			String decodeJsonParam = this.decodeJsonParam(resultJson);
			System.out.println(decodeJsonParam);
			// 转成实体对象
			authPersonal = JacksonUtil.Json2Obj(decodeJsonParam, new TypeReference<HlReturnInfoVo>() {
			});
		} catch (Exception e) {
			if (e instanceof ProductCanNotApplyException) {
				throw new ProductCanNotApplyException(e.getMessage());
			}
			e.printStackTrace();
			throw new BirdRequestException("11");
		}
	}

	/**
	 * 是否可交单new
	 * 
	 * @param submitDto
	 * @return
	 * @throws Exception
	 */
	@Test
	public void comitOrNots() throws UnsupportedEncodingException, Exception {
		@SuppressWarnings("unused")
		HlReturnInfoVo authPersonal = null;
		String url = "http://218.104.220.30:9999/api-facade/";
		try {
			Map<String, Object> ma = new HashMap<String, Object>();
			ma.put("reginMobile", "15692195786");// 范阳612727199405170038';362522198907295026#高雪15026423025
													// 231081199012020029
			ma.put("productType", "XYM");
			// logger.info(creditHLUrl.concat("check/isReceiveCustomers"));
			// 加密请求、
			String json = JacksonUtil.Obj2Json(ma);
			String code = this.encodeJsonParam(json);
			String resultJson = HttpRequest.doJson(url.concat("check/isCustomerRegisted"), code);
			// 解密数据
			//resultJson="KHyhNXAasEgGfIYG1e2bgK39/IoPTKHkfNJaFE0Lq1X22/QhUxuI2h+vj/LleksMVslZGYgpbUkc\r\netNwfjFfqTe+TPXAmDObZhyl6glBZy1AGXlOXkrGxVy/PIsJApGJIOtgaekUfJ6TeD788cT0fUo+\r\nBdmtidURn46fEhs5iII\u003d\r\n";
			String decodeJsonParam = this.decodeJsonParam(resultJson);
			System.out.println(decodeJsonParam);
			// 转成实体对象
			authPersonal = JacksonUtil.Json2Obj(decodeJsonParam, new TypeReference<HlReturnInfoVo>() {
			});
		} catch (Exception e) {
			if (e instanceof ProductCanNotApplyException) {
				throw new ProductCanNotApplyException(e.getMessage());
			}
			e.printStackTrace();
			throw new BirdRequestException("11");
		}
	}

	/**
	 * 授信结果查询
	 * 
	 * @param submitDto
	 * @return
	 * @throws Exception
	 */
	@Test
	public void grantQuery() throws Exception {
		HlReturnInfoVo authPersonal = null;
		// String url = "http://hl.vcash.cn/huanle/facade/";
		String url = "http://218.104.220.30:9999/api-facade/";

		try {
			Map<String, Object> ma = new HashMap<String, Object>();
			ma.put("identityNo", "362522198907295026");
			ma.put("productType", "XYM");
			String json = JacksonUtil.Obj2Json(ma);
			String code = this.encodeJsonParam(json);
			String resultJson = HttpRequest.doJson(url.concat("customer/queryCustomerCredit"), code);
			String decodeJsonParam = this.decodeJsonParam(resultJson);
			System.out.println(decodeJsonParam);
			authPersonal = JacksonUtil.Json2Obj(decodeJsonParam, new TypeReference<HlReturnInfoVo>() {
			});
		} catch (Exception e) {
			e.printStackTrace();
			if (e instanceof ProductCanNotApplyException) {
				throw new ProductCanNotApplyException(e.getMessage());
			}
		}
	}

	/**
	 * 提交资料
	 * 
	 * @param submitDto
	 * @return
	 * @throws Exception
	 */
	@Test
	public void commit() throws Exception {
		String url = "http://218.104.220.30:9999/huanle/facade/";

		HlReturnInfoVo authPersonal = null;
		try {
			String json = "{" + "    \"reginMobile\":\"15692195786\"," + "    \"reginPassword\":\"12345678\"," + "    \"invitationCode\":\"TQ1828\","
					+ "    \"frontImage\":\"http://api.creditcat.cn/test/api/resourceImage/BDB3A5D12914CB4CB2D313AB8B6543B2\","
					+ "    \"backImage\":\"http://api.creditcat.cn/test/api/resourceImage/2780EB1CACB82360A32016EEE9ACEE15\","
					+ "    \"liveBody\":\"http://api.creditcat.cn/test/api/resourceImage/9992D2306B3ADB08F287413A447CE709\"," + "    \"name\":\"蒋圆 \"," + "    \"age\":\"28\"," + "    \"sex\":\"女\","
					+ "    \"identityNo\":\"510521198909191431\"," + "    \"householdAddr\":\"江西省抚州市南城县徐家乡五帝村黎家源组20号\"," + "    \"houseHold\":\"HOUSEHOLD/FEIBENDIJI\"," + "    \"address\":\"310000\","
					+ "    \"marriage\":\"NEWMARRIAGE/WH\"," + "    \"jobOnline\":\"JOBONLINE/QITA\"," + "    \"education\":\"NEWEDUCATION/BK\"," + "    \"payHousingFund\":\"是\"," + "    \"facePairResult\":\"80\","
					+ "    \"mobileNo\":\"13628049819\"," + "    \"mobileReport\":\"jxl_18795179\"," + "    \"mobileScore\":\"65\"," + "    \"lendingCashCardBank\":\"BANKLIST/ZHAOHANG\","
					+ "    \"lendingCashCardNo\":\"4392268322311326\"," + "    \"lendingMobile\":\"15692195786\"," + "    \"creditReportId\":\"123456\"," + "    \"creditReportNo\":\"123456\","
					+ "    \"signImage\":\"http://api.creditcat.cn/test/api/resourceImage/EE57F03647CB68971A147F89B20C0638\"," + "    \"reportType\":\"10\"," + "    \"productType\":\"XYM\"," + "    \"scorename\":\"11\","
					+ "    \"modulename\":\"22\"" + "}";
			String recodeJson = this.encodeJsonParam(json.toString());
			String jsons = this.decodeJsonParam(recodeJson);
			String resultJson = HttpRequest.doJson(url.concat("customer/submitInfo"), recodeJson);
			String decodeJsonParam = this.decodeJsonParam(resultJson);
			System.out.println(decodeJsonParam);
			authPersonal = JacksonUtil.Json2Obj(decodeJsonParam, new TypeReference<HlReturnInfoVo>() {
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
